import {
    ShowProps
} from './types';

import React from 'react';

import Content from './content';

import RootSiblings from 'react-native-root-siblings';

export default (
    content: any,
    props?: ShowProps
) => {
    const {
        style,
        fixed,
        onClosed,
        direction,
        maskOpacity,
        clickClose,
        backClose,
        enableBack,
    } = props || {};

    let _ref: any = null;
    let _sibling: any = null;

    const closeFn:any = () => {
        if(_ref) _ref.onClose()
    }

    const _content = (
        <Content
            style={style}
            fixed={fixed}
            children={content}
            ref={e => _ref = e}
            direction={direction}
            clickClose={clickClose}
            enableBack={enableBack}
            backClose={backClose}
            maskOpacity={maskOpacity}
            onClosed={() => {
                _sibling.destroy();
                if(!!onClosed) {
                    onClosed()
                }
            }}
        />
    )

    _sibling = (
        new RootSiblings(_content)
    )

    return closeFn;
}